package com.ews.onlineexchange.service.schedule;

import java.util.Calendar;
import java.util.Collection;
import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Singleton
@Startup
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Named
@ApplicationScoped
public class TimerCancelService {

	@Resource
	TimerService timerService;
		
	public void postConstruct() {
//		Collection<Timer> timers = timerService.getAllTimers();
//		for (Timer timer : timers) {
//			System.out.println(timer.getInfo());
////			System.out.println(timer.toString());			
//		}
		
		System.out.println("MARKMARKMARK");
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 1);
		
		Collection<Timer> timers = timerService.getAllTimers();
		System.out.println(timers.size());
		for (Timer timer : timers) {
			if (timer.isPersistent()) {
				System.out.println(timer.getInfo());
				timer.cancel();	
			}
		}
		Timer timer1 = timerService.createTimer(cal.getTime(), UUID.randomUUID().toString());
		System.out.println(timer1.isPersistent());
	}
	
	public void cancelAllTimers() {
		Collection<Timer> timers = timerService.getAllTimers();
		for (Timer timer : timers) {
			System.out.println(timer.getInfo());
			timer.cancel();
		}
	}
	
	@Timeout
	public void vvoid() {
		
	}
	
}
